package cn.yunhe.controller;

import cn.yunhe.model.Member;
import cn.yunhe.service.MemberBiz;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import util.Result;

import java.util.HashMap;
import java.util.Map;


/*spring注解*//*springMVC注解*/
@RestController(value = "memberAction111")
/*spring注解*//*springMVC注解*/
/*@Controller(value = "memberAction111")*/
public class MemberAction {

    /*spring注解*/
    @Autowired
    @Qualifier(value = "memberBiz")
    private MemberBiz memberBiz;

    //http://localhost:8080/memberList111.dojava
    /*springMVC注解*/
    //@RequestMapping(value = "/memberList111")
    public String memberList(){
        return "hhhhhh";
    }

    //http://localhost:8080/memberList111.dojava
    /*springMVC注解*/
    @RequestMapping(value = "/memberList111")
    public ModelAndView memberList1(){
        return new ModelAndView("memberInfoList");
    }

    //http://localhost:8080/searchByConditions111.dojava
    @RequestMapping(value = "/searchByConditions2Member")
    public Map searchByConditions(int pageNum, int pageSize, Member member){
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("member",member);
        Page page=memberBiz.searchByConditions(map);

        System.out.println("page======"+page);
        map.put("page",page);
        map.put("totalPages",page.getPages());
        return map;
    }

    @RequestMapping(value = "insertRecord111")
    public Result<Member> insertRecord(Member member) {
        Integer num2Record = memberBiz.insertRecord(member);
        if (num2Record > 0) {
            return new Result(1001,"插入成功");
        }else {
            return new Result(1002,"插入失败");
        }
    }

    @RequestMapping(value = "deleteRecord111")
    public Result deleteRecord(Member member){
        Integer num2Record=memberBiz.deleteRecord(member);
        if (num2Record==1){
            return new Result(1001,"删除唯一一条数据");
        }else if (num2Record>1){
            return new Result(1002,"删除多余1条数据");
        }else {
            return new Result(1003,"删除失败");
        }
    }

    @RequestMapping(value = "toDoUpdateRecord111")
    public ModelAndView toDoUpdateRecord(Member member){
        Member member1=memberBiz.getById(member);
        Map map=new HashMap();
        map.put("member111",member1);
        return new ModelAndView("toDoUpdateRecord",map);
    }

    @RequestMapping(value = "updateRecord111")
    public Result updateRecord(Member member){
        Integer num2Record=memberBiz.updateRecord(member);
        if (num2Record==1){
            return new Result(1001,"更新唯一一条数据");
        }else if (num2Record>1){
            return new Result(1002,"更新多余1条数据");
        }else {
            return new Result(1003,"更新失败");
        }
    }





    public MemberBiz getMemberBiz() {
        return memberBiz;
    }

    public void setMemberBiz(MemberBiz memberBiz) {
        this.memberBiz = memberBiz;
    }
}
